// 权限管理工具
class PermissionManager {
	constructor() {
		this.userInfo = null;
		this.roles = [];
		this.permissions = [];
	}
	
	// 设置用户信息
	setUserInfo(userInfo) {
		this.userInfo = userInfo;
		this.roles = userInfo.roles || [];
		this.permissions = userInfo.permissions || [];
		console.log('用户权限信息:', {
			user: userInfo.user?.userName,
			roles: this.roles,
			permissions: this.permissions,
			roleKey: this.getRoleKey()
		});
	}
	
	// 检查是否有指定角色
	hasRole(roleKey) {
		// 支持角色键和角色名称的检查
		if (!this.roles || this.roles.length === 0) {
			return false;
		}
		
		// 角色名称到角色键的映射
		const roleNameToKey = {
			'超级管理员': 'admin',
			'维修主管': 'maintenance_manager',
			'维修人员': 'maintenance_worker',
			'维修工长': 'maintenance_foreman',
			'仓库管理员': 'warehouse_manager',
			'普通员工': 'normal_user'
		};
		
		// 检查角色键
		if (this.roles.includes(roleKey)) {
			return true;
		}
		
		// 检查角色名称
		for (const role of this.roles) {
			if (roleNameToKey[role] === roleKey) {
				return true;
			}
		}
		
		return false;
	}
	
	// 检查是否有指定权限
	hasPermission(permission) {
		return this.permissions.includes(permission);
	}
	
	// 检查是否为管理员
	isAdmin() {
		return this.hasRole('admin');
	}
	
	// 检查是否为维修主管
	isMaintenanceManager() {
		return this.hasRole('maintenance_manager');
	}
	
	// 检查是否为维修人员
	isMaintenanceWorker() {
		return this.hasRole('maintenance_worker');
	}
	
	// 检查是否为仓库管理员
	isWarehouseManager() {
		return this.hasRole('warehouse_manager');
	}
	
	// 获取角色键
	getRoleKey() {
		if (!this.roles || this.roles.length === 0) {
			return 'common';
		}
		
		const role = this.roles[0];
		
		// 角色名称到角色键的映射
		const roleNameToKey = {
			'超级管理员': 'admin',
			'维修主管': 'maintenance_manager',
			'维修人员': 'maintenance_worker',
			'维修工长': 'maintenance_foreman',
			'仓库管理员': 'warehouse_manager',
			'普通员工': 'normal_user'
		};
		
		// 如果已经是角色键，直接返回
		if (roleNameToKey[role]) {
			return roleNameToKey[role];
		}
		
		// 如果已经是角色键，直接返回
		if (['admin', 'maintenance_manager', 'maintenance_worker', 'maintenance_foreman', 'warehouse_manager', 'normal_user'].includes(role)) {
			return role;
		}
		
		console.log('未知角色:', role);
		return 'common';
	}
	
	// 获取用户角色配置
	getRoleConfig() {
		const roleKey = this.getRoleKey();
		
		const roleConfigs = {
			admin: {
				name: '超级管理员',
				theme: 'admin',
				primaryColor: '#409EFF',
				secondaryColor: '#67C23A',
				accentColor: '#E6A23C',
				icon: '👑',
				features: {
					canViewAll: true,
					canManageUsers: true,
					canManageSystem: true,
					canViewReports: true,
					canManageAssets: true,
					canManageRepairs: true,
					canManageInventory: true,
					canManageWarehouse: true
				},
				menuItems: [
					{ key: 'dashboard', name: '工作台', icon: '📊', path: '/pages/index/index' },
					{ key: 'assets', name: '资产管理', icon: '📦', path: '/pages/asset/index' },
					{ key: 'fault-report', name: '故障报修', icon: '📋', path: '/pages/repair/fault-report' },
					{ key: 'repairs', name: '故障维修', icon: '🔧', path: '/pages/repair/index' },
					{ key: 'more', name: '更多', icon: '⋯', path: '/pages/more/index' }
				]
			},
			maintenance_manager: {
				name: '维修主管',
				theme: 'manager',
				primaryColor: '#409EFF',
				secondaryColor: '#67C23A',
				accentColor: '#E6A23C',
				icon: '👨‍💼',
				features: {
					canViewAll: true,
					canManageUsers: false,
					canManageSystem: false,
					canViewReports: true,
					canManageAssets: true,
					canManageRepairs: true,
					canManageInventory: false,
					canManageWarehouse: false
				},
				menuItems: [
					{ key: 'dashboard', name: '工作台', icon: '📊', path: '/pages/index/index' },
					{ key: 'assets', name: '资产管理', icon: '📦', path: '/pages/asset/index' },
					{ key: 'fault-report', name: '故障报修', icon: '📋', path: '/pages/repair/fault-report' },
					{ key: 'repairs', name: '故障维修', icon: '🔧', path: '/pages/repair/index' },
					{ key: 'more', name: '更多', icon: '⋯', path: '/pages/more/index' }
				]
			},
			maintenance_worker: {
				name: '维修人员',
				theme: 'worker',
				primaryColor: '#67C23A',
				secondaryColor: '#409EFF',
				accentColor: '#E6A23C',
				icon: '👷‍♂️',
				features: {
					canViewAll: false,
					canManageUsers: false,
					canManageSystem: false,
					canViewReports: false,
					canManageAssets: false,
					canManageRepairs: true,
					canManageInventory: false,
					canManageWarehouse: false
				},
				menuItems: [
					{ key: 'dashboard', name: '工作台', icon: '📊', path: '/pages/index/index' },
					{ key: 'assets', name: '资产管理', icon: '📦', path: '/pages/asset/index' },
					{ key: 'fault-report', name: '故障报修', icon: '📋', path: '/pages/repair/fault-report' },
					{ key: 'repairs', name: '故障维修', icon: '🔧', path: '/pages/repair/index' },
					{ key: 'more', name: '更多', icon: '⋯', path: '/pages/more/index' }
				]
			},
			maintenance_foreman: {
				name: '维修工长',
				theme: 'foreman',
				primaryColor: '#E6A23C',
				secondaryColor: '#67C23A',
				accentColor: '#409EFF',
				icon: '👷‍♂️',
				features: {
					canViewAll: false,
					canManageUsers: false,
					canManageSystem: false,
					canAssignRepairs: true,  // 可以指派维修
					canConfirmRepairs: true,  // 可以确认维修
					canViewReports: true,  // 可以查看报修记录
					canManageInventory: false,  // 不能管理库存
					canManageAssets: false  // 不能管理资产
				},
				menuItems: [
					{ key: 'fault-report', name: '故障报修', icon: '📋', path: '/pages/repair/fault-report' },
					{ key: 'repairs', name: '故障维修', icon: '🔧', path: '/pages/repair/index' },
					{ key: 'more', name: '更多', icon: '⋯', path: '/pages/more/index' }
				]
			},
			warehouse_manager: {
				name: '仓库管理员',
				theme: 'warehouse',
				primaryColor: '#E6A23C',
				secondaryColor: '#409EFF',
				accentColor: '#67C23A',
				icon: '📦',
				features: {
					canViewAll: false,
					canManageUsers: false,
					canManageSystem: false,
					canViewReports: true,
					canManageAssets: true,
					canManageRepairs: false,
					canManageInventory: true,
					canManageWarehouse: true
				},
				menuItems: [
					{ key: 'dashboard', name: '工作台', icon: '📊', path: '/pages/index/index' },
					{ key: 'assets', name: '资产管理', icon: '📦', path: '/pages/asset/index' },
					{ key: 'fault-report', name: '故障报修', icon: '📋', path: '/pages/repair/fault-report' },
					{ key: 'repairs', name: '故障维修', icon: '🔧', path: '/pages/repair/index' },
					{ key: 'more', name: '更多', icon: '⋯', path: '/pages/more/index' }
				]
			},
			normal_user: {
				name: '普通员工',
				theme: 'normal',
				primaryColor: '#909399',
				secondaryColor: '#409EFF',
				accentColor: '#67C23A',
				icon: '👤',
				features: {
					canViewAll: false,
					canManageUsers: false,
					canManageSystem: false,
					canViewReports: false,
					canManageAssets: false,
					canManageRepairs: false,
					canManageInventory: false,
					canManageWarehouse: false,
					canAddFaultReport: true,
					canViewFaultReport: true
				},
				menuItems: [
					{ key: 'dashboard', name: '工作台', icon: '📊', path: '/pages/index/index' },
					{ key: 'assets', name: '资产管理', icon: '📦', path: '/pages/asset/index' },
					{ key: 'fault-report', name: '故障报修', icon: '📋', path: '/pages/repair/fault-report' },
					{ key: 'repairs', name: '故障维修', icon: '🔧', path: '/pages/repair/index' },
					{ key: 'more', name: '更多', icon: '⋯', path: '/pages/more/index' }
				]
			},
			common: {
				name: '普通用户',
				theme: 'common',
				primaryColor: '#909399',
				secondaryColor: '#409EFF',
				accentColor: '#67C23A',
				icon: '👤',
				features: {
					canViewAll: false,
					canManageUsers: false,
					canManageSystem: false,
					canViewReports: false,
					canManageAssets: false,
					canManageRepairs: false,
					canManageInventory: false,
					canManageWarehouse: false
				},
				menuItems: [
					{ key: 'dashboard', name: '工作台', icon: '📊', path: '/pages/index/index' },
					{ key: 'assets', name: '资产管理', icon: '📦', path: '/pages/asset/index' },
					{ key: 'fault-report', name: '故障报修', icon: '📋', path: '/pages/repair/fault-report' },
					{ key: 'repairs', name: '故障维修', icon: '🔧', path: '/pages/repair/index' },
					{ key: 'more', name: '更多', icon: '⋯', path: '/pages/more/index' }
				]
			}
		};
		
		return roleConfigs[roleKey] || roleConfigs.common;
	}
	
	// 获取主题样式
	getThemeStyles() {
		const config = this.getRoleConfig();
		return {
			'--primary-color': config.primaryColor,
			'--secondary-color': config.secondaryColor,
			'--accent-color': config.accentColor,
			'--theme-name': config.theme
		};
	}
	
	// 检查功能权限
	canAccess(feature) {
		const config = this.getRoleConfig();
		return config.features[feature] || false;
	}
	
	// 获取菜单项
	getMenuItems() {
		const config = this.getRoleConfig();
		return config.menuItems || [];
	}
	
	// 获取角色特定的首页路径
	getRoleHomePage() {
		const roleKey = this.getRoleKey();
		
		const homePages = {
			admin: '/pages/index/index',
			maintenance_manager: '/pages/repair/fault-report',  // 维修主管直接进入故障报修页面
			maintenance_worker: '/pages/repair/index',  // 维修人员直接进入我的维修页面
			maintenance_foreman: '/pages/repair/fault-report',  // 维修工长直接进入故障报修页面
			warehouse_manager: '/pages/warehouse/dashboard',
			normal_user: '/pages/index/index',
			common: '/pages/index/index'
		};
		
		return homePages[roleKey] || homePages.common;
	}
	
	// 检查是否应该重定向到角色特定页面
	shouldRedirectToRolePage() {
		const roleKey = this.getRoleKey();
		return ['maintenance_manager', 'maintenance_worker', 'maintenance_foreman', 'warehouse_manager'].includes(roleKey);
	}
	
	// 获取用户显示名称
	getUserDisplayName() {
		if (!this.userInfo || !this.userInfo.user) return '未知用户';
		return this.userInfo.user.nickName || this.userInfo.user.userName;
	}
	
	// 获取角色显示名称
	getRoleDisplayName() {
		const config = this.getRoleConfig();
		return config.name;
	}
	
	// 获取角色图标
	getRoleIcon() {
		const config = this.getRoleConfig();
		return config.icon;
	}
}

// 创建全局权限管理器实例
const permissionManager = new PermissionManager();

export default permissionManager;
